<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>


<%@ page import="javax.portlet.*"%>
<%@page import="com.liferay.portal.model.User" %>
<%@page import="com.liferay.portal.util.PortalUtil" %>
<%@page import="com.jefe.hudson.service.service.*" %>
<%@page import="com.jefe.hudson.service.model.*" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<%--
Author: Sonali Mendis
Code:   Hudson Portlet for Jefe (http://www.jefesolutions.com)
License:GNU
--%>

<portlet:defineObjects />
<%-- Get User Preferences--%>
<%
    long id = 0;
    User currentUser = null;
    String host  = "";
    int port = 0;
    String user = "";
    String pass = "";
    Boolean userSet = false;
    try {
        currentUser = PortalUtil.getUser(request);
        id = currentUser.getUserId();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try{
    UserPrefs userDtls = UserPrefsLocalServiceUtil.getUserPrefs(id);
    host = userDtls.getHudHost();
    port = userDtls.getHudPort();
    user = userDtls.getHudUser();
    pass = userDtls.getHudPass();
    userSet = true;
    }catch(Exception ex){
        System.out.println(ex.getMessage());
    }
%>
<%-- Get error pr success messages--%>
<%
String error = renderRequest.getParameter("error");
String success = renderRequest.getParameter("success");

if(error!=null){ //if there are any error messages display here
    out.println("<div class=\"portlet-msg-error\">"+error.replace("+", " ")+"</div>");
}
if(success!=null){ // if there are any success messages display here
    out.println("<div class=\"portlet-msg-success\">"+success.replace("+", " ")+"</div>");
}
%> 
<%-- anyway Display the form here--%>
<div class="portlet-form">
    <form action="<portlet:actionURL><portlet:param name="edit" value="edit"/></portlet:actionURL>" method="post">
        <table>
                    <tr>
                        <td><span class="portlet-form-label">Hudson host*&nbsp;</span></td>
                        <td>:</td>
                        <td><input class="portlet-form-input-field" type="text" name="formHost" value="<%=host %>" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"></td>
                        <td><div class="portlet-font" style="font-size: xx-small">(Eg 1: www.jefe-hudson.com, eg 2: 10.0.0.1, eg 3:localhost)</div></td>
                    </tr>
                    <tr>
                        <td><span class="portlet-form-label">Hudson port*&nbsp;</span></td>
                        <td>:</td>
                        <td><input class="portlet-form-input-field" type="text" name="formPort" value="<%=port %>" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"></td>
                        <td><div class="portlet-font" style="font-size: xx-small">(Eg 1: 433, eg 2: 8080 - It is the port at which hudson server is running.)</div></td>
                    </tr>
                    <tr>
                        <td><span class="portlet-form-label">Hudson Username&nbsp;</span></td>
                        <td>:</td>
                        <td><input class="portlet-form-input-field" type="text" name="formUser" value="<%=user %>" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"></td>
                        <td><div class="portlet-font" style="font-size: xx-small">(Leave this blank if security is not enabled for Hudson server. <br/>If secuiry is enabled at Hudson server, input your hudson username when using the portlet for the first time.)</div></td>
                    </tr>
                    <tr>
                        <td><span class="portlet-form-label">Hudson Password&nbsp;</span></td>
                        <td>:</td>
                        <td><input type="password" class="portlet-form-input-field" type="text" name="formPass" value="<%=pass %>" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"></td>
                        <td><div class="portlet-font" style="font-size: xx-small">(Leave this blank if security is not enabled for Hudson server. <br/>If secuiry is enabled at Hudson server, input your hudson password when using the portlet for the first time)</div></td>
                    </tr>
                    <tr><td></td><td></td></tr>
                    <tr><td colspan="3" align="right"><input class="portlet-form-button" type="submit" value="Submit" /></td></tr>
        </table>
    </form>
</div>
<%-- Footer to link to Hudson --%>
<%
    if(userSet){
%>
<hr style="border-bottom: dotted 1px"/>
<div class="portlet-section-footer" >
    More on: <a href="http://<%=host%>:<%=port%>/hudson" >Hudson</a>
</div>
<%}%>